package com.ddsso.auth.web.repository.impl;

import org.apache.shiro.dao.DataAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.ddsso.auth.web.util.SerializeUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author zdd
 * @package com.ddsso.repository.impl
 * @Time Created by zdd on 2016/10/26.
 * @description To-do
 */
public abstract class BaseDaoImpl<T extends Object> {

    private static Logger logger = LoggerFactory.getLogger(BaseDaoImpl.class);
    @Resource
    protected JedisPool jedisPool;
    //操作redis客户端
    private static Jedis jedis;
    /**
     * 获取一个jedis 客户端,单一模式
     * @return
     */
    private synchronized  Jedis getJedis(){
        Jedis jedis = null;
        try {
            if (jedisPool != null) {
                jedis = jedisPool.getResource();
            }
        } catch (Exception e) {
            logger.error("Get jedis error : "+e);
        }finally{
            returnResource(jedis);
        }
        return jedis;
    }

    /**
     * 释放jedis资源
     * @param jedis
     */
    private synchronized  void returnResource(final Jedis jedis) {
        if (jedis != null && jedisPool !=null) {
            jedisPool.returnResource(jedis);
        }
    }



    /**
     * 批量删除对应的value
     * @param keys
     */
    protected synchronized void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }


    /**
     * 删除对应的value     *     * @param key
     */
    protected synchronized void remove(final String key) {
        if (exists(key)) {
            this.getJedis().del(key);
        }

    }

    /**
     * 判断缓存中是否有对应的value
     * @param key
     * * @return
     */
    protected synchronized boolean exists(final String key) {
        return this.getJedis().exists(key);
    }

    /**
     * 读取缓存
     * @param key
     * @return
     */
    protected synchronized T get(final String key) {
        byte [] arr=this.getJedis().get(key.getBytes());
        if(arr!=null){
            return (T)SerializeUtil.unserialize(arr);
        }else{
            return null;
        }
    }

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    protected synchronized boolean set(final String key, T value) {
        boolean result = false;
        try {
            this.getJedis().set(key.getBytes(),SerializeUtil.serialize(value));
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return result;
        }

    }

    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    protected synchronized boolean set(final String key, T value, Long expireTime) {
        boolean result = false;
        try {
            this.getJedis().set(key.getBytes(),SerializeUtil.serialize(value));
            this.getJedis().expire(key,expireTime.intValue());
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return result;
        }

    }

    public synchronized  void save(final String key, Object value) {
        set(key, (T) value);
    }


    public synchronized  <T> T fetch(final String key, Class<T> elementType) {
        return (T)this.get(key);
    }


}
